package com.easygem.controller.front;

import com.easygem.common.annotation.ControllerEndpoint;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.front.ClientType;
import com.easygem.common.model.front.Industry;
import com.easygem.common.model.front.LabelInfo;
import com.easygem.common.model.front.ResourceInfo;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.vo.front.ResourceInfoVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.service.*;
import com.easygem.system.service.UserService;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 何维佳
 * @version 1.0
 * @className ResourceInfoController
 * @Description TODO
 * @date 2024/12/23 18:41
 */
@Api(tags = "资源管理")
@RestController
@RequestMapping("/resourceInfo")
public class ResourceInfoController {

    @Autowired
    private ResourceInfoService resourceInfoService;

    @Autowired
    private IndustryService industryService;

    @Autowired
    private FrontClientTypeService clienttypeService;

    @Autowired
    private UserService usersService;

    @Autowired
    private LabelInfoService labelInfoService;

    @Autowired
    private CustomerService customerService;

    /**
     * 资源列表
     *
     * @return
     */
    @ApiOperation(value = "资源列表", notes = "模糊查询持有人")
    @GetMapping("/findResourceInfo")
    @ResponseBody
    public ResponseBean<PageVO<ResourceInfo>> findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                                                  ResourceInfoVO resourceInfoVO) {

        //
        PageVO<ResourceInfo> resourceInfoVOPageVO = resourceInfoService.findList(pageNum, pageSize, resourceInfoVO);
        resourceInfoVOPageVO.setTotal(resourceInfoVOPageVO.getTotal());

//// 遍历PageVO<ResourceInfo>对象的属性
//        try {
//            Field[] fields = PageVO.class.getDeclaredFields();
//            for (Field field : fields) {
//                field.setAccessible(true);
//                Object value = field.get(resourceInfoVOPageVO);
////                System.out.println(field.getName() + " : " + value);
//                if (field.getName().equals("list")) {
//                    // 假设list属性是存放ResourceInfo的列表，也进行遍历
//                    List<ResourceInfo> resourceInfoList = (List<ResourceInfo>) value;
//                    for (ResourceInfo resourceInfo : resourceInfoList) {
//                        Field[] resourceInfoFields = ResourceInfo.class.getDeclaredFields();
//                        for (Field resourceInfoField : resourceInfoFields) {
//                            resourceInfoField.setAccessible(true);
//                            Object resourceInfoValue = resourceInfoField.get(resourceInfo);
////                            System.out.println("    ResourceInfo - " + resourceInfoField.getName() + " : " + resourceInfoValue);
//                        }
//                    }
//                }
//            }
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
        return ResponseBean.success(resourceInfoVOPageVO);
    }

    /**
     * 行业列表
     *
     * @return
     */
    @ApiOperation(value = "行业列表", notes = "模糊查询持有人")
    @GetMapping("/findIndustryVOList")
    @ResponseBody
    public List<Industry> findIndustryVOList(@RequestParam(value = "accountName") String accountName) {
        List<Industry> industryList = industryService.findIndustryList(accountName);
        return industryList;
    }

    /**
     * 标签列表
     *
     * @return
     */
    @ApiOperation(value = "标签列表", notes = "标签")
    @GetMapping("/findLabelInfoList")
    @ResponseBody
    public List<LabelInfo> findLabelInfoList(@RequestParam(value = "accountName") String accountName) {
        List<LabelInfo> labelInfoList = labelInfoService.findLabelInfoList(accountName);
        return labelInfoList;
    }

    /**
     * 客人种类列表
     *
     * @return
     */
    @ApiOperation(value = "资源列表", notes = "模糊查询持有人")
    @GetMapping("/findClienttypeList")
    @ResponseBody
    public List<ClientType> findClienttypeList(@RequestParam(value = "accountName") String accountName) {
        List<ClientType> clientTypeList = clienttypeService.findClienttypeList(accountName);
        return clientTypeList;
    }

//    /**
//     * 归属人列表
//     *
//     * @return
//     */
//    @ApiOperation(value = "归属人列表", notes = "模糊查询当前部门所有归属人")
//    @GetMapping("/findUsersList")
//    @ResponseBody
//    public List<User> findUsersList(@RequestParam(value = "accountName") String accountName) {
//        List<User> usersList = usersService.findUsersList(accountName);
//        return usersList;
//    }


    /**
     * 更具id查取资源信息
     *
     * @return
     */
    @ApiOperation(value = "资源列表", notes = "通过Id查询资源信息")
    @GetMapping("/findResourceListById")
    @ResponseBody
    public List<ResourceInfo> findResourceListById(@RequestParam(value = "id") Long Id) {
        System.out.println(Id);
        List<ResourceInfo> resourceInfoList = resourceInfoService.findListById(Id);
        return resourceInfoList;
    }
    /**
     * 更新资源
     *
     * @return
     */
    @ApiOperation(value = "更新资源", notes = "更新")
    @PostMapping("/updateResourceListById")
    @ResponseBody
    @RequiresPermissions("resource:updateResource")
    public  ResponseBean<ResourceInfoVO>  updateResourceInfo(@RequestBody ResourceInfoVO resourceInfoVO) {
        ResourceInfo r = resourceInfoService.findListById(resourceInfoVO.getResourceId()).get(0);
        String PhoneA = resourceInfoVO.getPhoneA();
        String PhoneB = resourceInfoVO.getPhoneB();
        if (r.getPhoneA().equals(PhoneA)) {
            resourceInfoVO.setPhoneA(null);
        }
        if (PhoneB != null && r.getPhoneB() != null) {
            if (r.getPhoneB().equals(PhoneB)) {
                resourceInfoVO.setPhoneB(null);
            }
        }

        Integer isExistPhone = customerService.isExistPhone(resourceInfoVO.getPhoneA(),resourceInfoVO.getPhoneB(),resourceInfoVO.getAccountName());
        System.out.println(resourceInfoVO.getAccountName()+"1111111111111");
        if (isExistPhone <= 0) {
            System.out.println("进行修改操作");
            resourceInfoService.update(resourceInfoVO);
            return ResponseBean.success(resourceInfoVO);
        }
     return ResponseBean.success(null);
    }

    /**
     * 加入黑名单
     *
     * @return
     */
    @ApiOperation(value = "加入黑名单", notes = "加入黑名单")
    @PostMapping("/insertBlackList")
    @ResponseBody
    @RequiresPermissions("resource:insertBlackList")
    public int insertBlackList (@RequestBody ResourceInfoVO resourceInfoVO) {
        int result = resourceInfoService.insertBlackList(resourceInfoVO);
        return result;
    }

    /**
     * 外呼
     *
     * @return
     */
    @ApiOperation(value = "外呼", notes = "外呼")
    @GetMapping("/callResource")
    @ResponseBody
    @RequiresPermissions("resource:callResource")
    public int callResource (@RequestParam (value = "clientPhone") String clientPhone) {
        int result = 0;
        return result;
    }

    /**
     * 判断是否为黑名单
     *
     * @return
     */
    @ApiOperation(value = "判断是否为黑名单", notes = "判断是否为黑名单")
    @GetMapping("/isBlacklist")
    @ResponseBody
    public int isBlacklist (@RequestParam (value = "clientPhone") String clientPhone) {
        int result = resourceInfoService.isBlacklist(clientPhone);
        return result;
    }

    /**
     * 加入公海
     *
     * @return
     */
    @ApiOperation(value = "加入公海", notes = "加入公海")
    @GetMapping("/insertHigh_seas_clientList")
    @ResponseBody
    @RequiresPermissions("resource:insertHigh_seas")
    public int insertHigh_seas_clientList (@RequestParam(value = "id") Long id) {
        int result = resourceInfoService.insertHigh_seas_clientList(id);
        return result;
    }


    /**
     * 删除资源
     *
     * @return
     */
    @ApiOperation(value = "删除资源", notes = "删除")
    @GetMapping("/deleteResourceInfoById")
    @ResponseBody
    @RequiresPermissions("resource:delete")
    public int deleteResourceInfoById(@RequestParam(value = "id") Long id) {
        int result = resourceInfoService.delete(id);
        System.out.println(result);
        return result;
    }

    /**
     * 批量导出excel
     * @param response
     * @param ids
     */
    @ApiOperation(value = "批量导出excel", notes = "根据传入的id列表批量导出用户的excel表格")
    @PostMapping("/excelBatch")
    @RequiresPermissions("resource:export")
    @ControllerEndpoint(exceptionMessage = "批量导出Excel失败", operation = "批量导出用户excel")
    public void batchExport(HttpServletResponse response,
                            @RequestBody List<Long> ids) {
        List<ResourceInfo> resourceInfoList = this.resourceInfoService.findAllByIds(ids);
        ExcelKit.$Export(ResourceInfo.class, response).downXlsx(resourceInfoList, false);
    }
    /**
     * 回收站的资源列表
     *
     * @return
     */
    @ApiOperation(value = "回收站的资源列表", notes = "回收站的资源列表")
    @GetMapping("/IsDeleteFindList")
    @ResponseBody
    public ResponseBean<PageVO<ResourceInfo>> DeleteFindList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                             ResourceInfoVO resourceInfoVO) {
        PageVO<ResourceInfo> resourceInfoVOPageVO = resourceInfoService.IsDeleteFindList(pageNum, pageSize, resourceInfoVO);
        return ResponseBean.success(resourceInfoVOPageVO);
    }

    /**
     * 根据条件清空进入回收站的资源
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "根据条件清空回收站的资源失败", operation = "根据条件清空回收站的资源")
    @ApiOperation(value = "清空回收站的资源")
    @RequiresPermissions({"resource:restore"})
    @PostMapping("/deletesByResourceInfoVO")
    @ResponseBody
    public ResponseBean deletesByResourceInfoVO(@RequestBody @Validated ResourceInfoVO resourceInfoVO){
        resourceInfoService.deleteResourceInfo(resourceInfoVO);
        return ResponseBean.success();
    }
    /**
     * 导出回收站的资源excel
     * @param response
     */
    @ApiOperation(value = "导出excel", notes = "导出所有进入回收站的资源的excel表格")
    @PostMapping("/resourceInfoExcel")
    @RequiresPermissions("resource:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出回收站的资源excel")
    public void export(HttpServletResponse response,@RequestBody ResourceInfoVO resourceInfoVO) {
        List<ResourceInfo> resourceInfoList = this.resourceInfoService.findByResourceInfoAll(resourceInfoVO);
        ExcelKit.$Export(ResourceInfo.class, response).downXlsx(resourceInfoList, false);
    }








    /**
     * 回收站的客户列表
     *
     * @return
     */
    @ApiOperation(value = "回收站的客户列表", notes = "回收站的客户列表")
    @GetMapping("/IsDeleteCustomerList")
    @ResponseBody
    public ResponseBean<PageVO<ResourceInfo>> DeleteCustomer(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                             ResourceInfoVO resourceInfoVO) {
        PageVO<ResourceInfo> resourceInfoVOPageVO = resourceInfoService.selectAllCustomer(pageNum, pageSize, resourceInfoVO);
        return ResponseBean.success(resourceInfoVOPageVO);
    }

    /**
     * 根据条件清空进入回收站的客户
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "根据条件清空回收站的客户失败", operation = "根据条件清空回收站的客户")
    @ApiOperation(value = "清空回收站的客户")
    @RequiresPermissions({"customer:erase"})
    @PostMapping("/deletesByCustomer")
    @ResponseBody
    public ResponseBean deletesByCustomer(@RequestBody @Validated ResourceInfoVO resourceInfoVO){
        resourceInfoService.deleteCustomer(resourceInfoVO);
        return ResponseBean.success();
    }
    /**
     * 批量删除或删除
     */
    @ControllerEndpoint(exceptionMessage = "删除回收站的客户失败", operation = "删除回收站的客户")
    @ApiOperation(value = "删除回收站的客户")
    @RequiresPermissions({"customer:delete"})
    @DeleteMapping("/deleteCustomer/{id}")
    @ResponseBody
    public ResponseBean delete(@PathVariable List<Long> id) throws SystemException {
        resourceInfoService.deleteCustomerId(id);
        return ResponseBean.success();
    }
    /**
     * 恢复回收站的客户操作
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "恢复回收站的客户失败", operation = "恢复回收站的客户")
    @ApiOperation(value = "恢复回收站的客户")
    @RequiresPermissions({"customer:restore"})
    @PutMapping("/updateIsDelete/{id}")
    @ResponseBody
    public ResponseBean updateIsDelete(@PathVariable List<Long> id) {
        resourceInfoService.updateIsDelete(id);
        return ResponseBean.success();
    }
    /**
     * 批量批量分配或分配
     */
    @ControllerEndpoint(exceptionMessage = "批量分配或分配回收站的客户失败", operation = "批量分配或分配回收站的客户")
    @ApiOperation(value = "批量分配或分配回收站的客户")
    @RequiresPermissions({"customer:assign"})
    @PostMapping("/distributeCustomer/{resourceInfoId}")
    @ResponseBody
    public ResponseBean distributeCustomer( @PathVariable List<Long> resourceInfoId,@RequestBody  @Validated List<Long> userId) throws SystemException {
        System.out.println("***************88");
        resourceInfoService.distributeCustomer(userId,resourceInfoId);
        return ResponseBean.success();
    }







    /**
     * 公海预警列表
     *
     * @return
     */
    @ApiOperation(value = "公海预警列表", notes = "公海预警列表")
    @GetMapping("/selectHighSeasClient")
    @ResponseBody
    public ResponseBean<PageVO<ResourceInfo>> selectHighSeasClient(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                                   ResourceInfoVO resourceInfoVO) {
        PageVO<ResourceInfo> resourceInfoVOPageVO = resourceInfoService.selectHighSeasClient(pageNum, pageSize, resourceInfoVO);
        return ResponseBean.success(resourceInfoVOPageVO);
    }



    /**
     * 回收站预警列表
     *
     * @return
     */
    @ApiOperation(value = "回收站预警列表", notes = "回收站预警列表")
    @GetMapping("/selectRecycle")
    @ResponseBody
    public ResponseBean<PageVO<ResourceInfo>> selectRecycle(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                            ResourceInfoVO resourceInfoVO) {
        PageVO<ResourceInfo> resourceInfoVOPageVO = resourceInfoService.selectRecycle(pageNum, pageSize, resourceInfoVO);
        return ResponseBean.success(resourceInfoVOPageVO);
    }
}
